package com.fjrz.bbxwj.main.notice

import android.app.Activity
import android.content.Intent
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.Observer
import com.blankj.utilcode.util.ConvertUtils
import com.module.lemlin.custom.SpacingItemDecoration
import com.fjrz.bbxwj.R
import com.fjrz.bbxwj.entity.Announcement
import com.fjrz.bbxwj.entity.MessageSystem
import com.fjrz.bbxwj.main.notice.adapter.MessageSystemAdapter
import com.fjrz.bbxwj.mode.vm.MessageViewModel
import com.module.lemlin.mode.ViewModeActivity
import com.module.lemlin.rxhttp.Status
import com.module.lemlin.rxhttp.msg
import kotlinx.android.synthetic.main.activity_message_bulletin.*
import kotlinx.android.synthetic.main.activity_message_system.*
import kotlinx.android.synthetic.main.activity_message_system.ivSMessageBulletinBack
import kotlinx.android.synthetic.main.activity_message_system.mSmartRefreshLayout

class MessageSystemActivity : ViewModeActivity<MessageViewModel>() {

    companion object {
        fun open(activity: Activity) {
            val intent = Intent(activity, MessageSystemActivity::class.java)
            activity.startActivity(intent)
        }
    }

    private val mMessageSystemAdapter =
        MessageSystemAdapter()

    override fun viewModel(): MessageViewModel {
        return createViewModel()
    }

    override val layoutResId: Int
        get() = R.layout.activity_message_system

    override fun stateBar(): StatusBar {
        return StatusBar(
            isLightMode = true,
            color = R.color.colorWhite,
            view = flMessageSystemBar
        )
    }

    override fun initView() {
        ivSMessageBulletinBack.setOnClickListener { this.finish() }

        rvMessageSystemBody.apply {
            val decoration = SpacingItemDecoration(
                vGap = ConvertUtils.dp2px((15f)),
                hGap = ConvertUtils.dp2px((15f))
            )
            addItemDecoration(decoration)
        }.adapter = mMessageSystemAdapter

        mMessageSystemAdapter.setOnItemClickListener { _, view, position ->
            val itemData: MessageSystem = mMessageSystemAdapter.getItem(position)
            MessageDetailsActivity.openSystem(activity = this, id = itemData.id)
        }

        //下拉刷新监听
        mSmartRefreshLayout.setOnRefreshListener {
            viewModel.messageSystem()
        }
        //加载更多刷新监听
        mSmartRefreshLayout.setOnLoadMoreListener {
            viewModel.messageSystem(isLoadMore = true)
        }
    }

    override fun initData() {
        viewModel.messageSystem()
    }

    override fun initViewMode() {
        viewModel.messageSystemLiveData.observe(
            this as LifecycleOwner,
            Observer { response ->

                val status: Status = responseStatus1LiveData(response.status, response.throwable)
                if (status != Status.SUCCESS)
                    return@Observer
                val isLoadMore: Boolean = response.httpData?.loadMore ?: false
                val videoDrafts: MutableList<MessageSystem> =
                    response.httpData?.data?.data ?: mutableListOf()
                if (videoDrafts.isNullOrEmpty()) {
                    mMessageSystemAdapter.setEmptyView(createViewEmptyNull(hint = "暂无数据"))
                }
                if (isLoadMore) {
                    mMessageSystemAdapter.addData(videoDrafts)
                } else {
                    mMessageSystemAdapter.setNewInstance(videoDrafts)
                }
                if (videoDrafts.size < viewModel.limit) {
                    mMessageSystemAdapter.setFooterView(createViewFooter())
                    mSmartRefreshLayout.setEnableLoadMore(false)
                } else {
                    mMessageSystemAdapter.removeAllFooterView()
                    mSmartRefreshLayout.setEnableLoadMore(true)
                }
            })
    }

    private fun responseStatus1LiveData(status: Status, throwable: Throwable?): Status {
        if (status == Status.START) {
            mMessageSystemAdapter.setEmptyView(createViewEmptyLoad())
        } else if (status == Status.FAILURE) {
            mSmartRefreshLayout.finishLoadMore((false))
            mSmartRefreshLayout.finishRefresh((false))
            mMessageSystemAdapter.setEmptyView(
                createViewEmptyNull(
                    hint = throwable?.msg ?: "未知错误"
                )
            )
        } else {
            mSmartRefreshLayout.finishLoadMore((true))
            mSmartRefreshLayout.finishRefresh((true))
        }
        return status
    }
}